#include <iostream>
#include <memory>

#include "TcpServer.hpp"
#include "Socket.hpp"
#include "Accepter.hpp"
#include "Connection.hpp"

int main(int argc, char* argv[])
{
    int port = std::stoi(argv[1]);

    //创建listen套接字
    std::unique_ptr<TcpSocket> listensock = std::make_unique<TcpSocket>(); //普通套接字
    listensock->BuildListenSocket(port, backlog);  //设置为lieten套接字
    // SetNonBlock(listensock->GetSockfd());
    // std::cout << "test\n" << std::endl;

    std::unique_ptr<TcpServer> svr = std::make_unique<TcpServer>();
    std::unique_ptr<Accepter> accepter = std::make_unique<Accepter>();
    auto listen_recver = std::bind(&Accepter::AccepterConntion, accepter.get(), std::placeholders::_1);
    
    Connection* conn = ConnectionFactory::BulidListenConnection(listensock->GetSockfd(),
        listen_recver, EPOLLIN | EPOLLET, svr.get());
    // std::cout << "test\n" << std::endl;

    svr->AddConnetion(conn);
    svr->Dispatcher();
}       